<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006 - 2009 The Bitsand Project (http://bitsand.googlecode.com/)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

//Do not check that user is logged in
$bLoginCheck = False;
//Initialise $CSS_PREFIX
$CSS_PREFIX = '../';

include ($CSS_PREFIX . 'inc/inc_head_db.php');
include ($CSS_PREFIX . 'inc/inc_head_html.php');
//Report all errors except E_NOTICE
error_reporting (E_ALL ^ E_NOTICE);
?>

<h1>Update Database</h1>

<p>
This page will update an existing Bitsand database (from Bitsand v4.x) to work with the latest version of Bitsand (version 5.0). It will report progress as it goes. Note that, in order for this to work, the user defined in the configuration file must have permission to CREATE, ALTER and DROP tables in the specified database. To use it, enter the value of CRYPT_KEY from the configuration file and click &quot;Update&quot;.
</p>

<form action = "update_db.php" method = "post">
<p>
Value of CRYPT_KEY in configuration file: <input name = "txtKey" type = "password"><br>
<input type = "submit" value = "Update" name = "btnSubmit">
</p>
</form>

<p>
<?php
if ($_POST ['btnSubmit'] != '' && $_POST ['txtKey'] == CRYPT_KEY && CheckReferrer ('update_db.php')) {
	$db_prefix = DB_PREFIX;

	echo "Adding 'Beguile &lt;X&gt;' to: {$dbprefix}osps<br />\n";
	if (ba_db_query ($link, "INSERT INTO `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Beguile <X>', 'Beguile <X>')") === False)
		echo "<span class = 'sans-warn'>Error adding 'Beguile <X>' into {$db_prefix}osps</span><br />\n";
	else
		echo "'Beguile &lt;X&gt;' added to: {$db_prefix}osps<br />\n";

	echo "Adding 'Event pack by post' field to: {$dbprefix}players<br />\n";
	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}players` ADD `plEventPackByPost` tinyint(1)") === False)
		echo "<span class = 'sans-warn'>Error adding 'Event pack by post' field to {$dbprefix}players</span><br />\n";
	else
		echo "'Event pack by post' field added to: {$dbprefix}players<br />\n";
	echo "Adding 'Monster Only' field to: {$dbprefix}characters<br />\n";

	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}characters` ADD `chMonsterOnly` tinyint(1)") === False)
		echo "<span class = 'sans-warn'>Error adding 'Monster Only' field to {$dbprefix}characters</span><br />\n";
	else
		echo "'Monster Only' field added to: {$dbprefix}characters<br />\n";

	echo "Setting default 'Monster Only' values<br />\n";
	if (ba_db_query ($link, "UPDATE `{$db_prefix}characters` SET `chMonsterOnly` = 1 where `chName` like '%monster%'") === False)
		echo "<span class = 'sans-warn'>Error updating 'Monster Only' field</span><br />\n";
	else
		echo "'Monster Only' field defaults set<br />\n";

	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}config` add `cnUSE_SHORT_OS_NAMES` tinyint(1)") === False)
		echo "<span class = 'sans-warn'>Error adding 'Use Short OS Name' field to {$dbprefix}config</span><br />\n";
	else
		echo "'Use Short OS Name' field added to: {$dbprefix}config<br />\n";

	echo "Setting default 'Use Short OS Name' value<br />\n";
	if (ba_db_query ($link, "UPDATE `{$db_prefix}config` SET `cnUSE_SHORT_OS_NAMES` = 1") === False)
		echo "<span class = 'sans-warn'>Error updating 'Use Short OS Name' field</span><br />\n";
	else
		echo "'Use Short OS Name' field defaults set<br />\n";

	echo "Updating High Magic OS<br/>\n";
	if (ba_db_query ($link, "update `{$db_prefix}osps` set `ospName` = 'High Magic (Channelling)', `ospShortName` = 'High Magic (Chan)' where `ospID` = 85;" === False))
		echo "<span class = 'sans-warn'>Error updating 'High Magic (Channelling)' OS</span><br />\n";
	else
		echo "'High Magic (Channelling)' OS updated<br />\n";

	if (ba_db_query ($link, "insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('High Magic (Spellcasting)', 'High Magic (Spell)'); insert into `{$db_prefix}ospstaken` (`otPlayerID`, `otOspID`) select {$db_prefix}characters.chPlayerID, LAST_INSERT_ID() from {$db_prefix}characters inner join {$db_prefix}ospstaken on {$db_prefix}characters.chPlayerID = {$db_prefix}ospstaken.otPlayerID left outer join {$db_prefix}skillstaken on {$db_prefix}characters.chPlayerID = {$db_prefix}skillstaken.stPlayerID  where otOspID = 85 and stSkillID = 27; delete {$db_prefix}ospstaken from {$db_prefix}ospstaken left outer join {$db_prefix}skillstaken on {$db_prefix}ospstaken.otPlayerID = {$db_prefix}skillstaken.stPlayerID  where otOspID = 85 and stSkillID = 27;" === False))
		echo "<span class = 'sans-warn'>Error creating 'High Magic (Spellcasting)' OS</span><br />\n";
	else
		echo "'High Magic (Spellcasting)' OS created<br />\n";

	if (ba_db_query ($link, "insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('High Magic (Incantation)', 'High Magic (Inc)'); insert into `{$db_prefix}ospstaken` (`otPlayerID`, `otOspID`) select {$db_prefix}characters.chPlayerID, LAST_INSERT_ID() from {$db_prefix}characters inner join {$db_prefix}ospstaken on {$db_prefix}characters.chPlayerID = {$db_prefix}ospstaken.otPlayerID left outer join {$db_prefix}skillstaken on {$db_prefix}characters.chPlayerID = {$db_prefix}skillstaken.stPlayerID  where otOspID = 85 and stSkillID = 31; delete {$db_prefix}ospstaken from {$db_prefix}ospstaken left outer join {$db_prefix}skillstaken on {$db_prefix}ospstaken.otPlayerID = {$db_prefix}skillstaken.stPlayerID  where otOspID = 85 and stSkillID = 31;" === False))
		echo "<span class = 'sans-warn'>Error creating 'High Magic (Incantation)' OS</span><br />\n";
	else
		echo "'High Magic (Incantation)' OS created<br />\n";

	if (ba_db_query ($link, "update `{$db_prefix}osps` set `ospName` = 'Vampire (Tier 1)', `ospShortName` = 'Vampire 1' where `ospName` = 'Vampire';insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Vampire (Tier 2)', 'Vampire 2');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Vampire (Tier 3)', 'Vampire 3');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Vampire (Tier 4)', 'Vampire 4');" === False))
		echo "<span class = 'sans-warn'>Error updating Vampire OS</span><br />\n";
	else
		echo "'Vampire' OS updated<br />\n";

	if (ba_db_query ($link, "update `{$db_prefix}osps` set `ospName` = 'Warlock (Tier 1)', `ospShortName` = 'Warlock 1' where `ospName` = 'Warlock';insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Warlock (Tier 2)', 'Warlock 2');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Warlock (Tier 3)', 'Warlock 3');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Warlock (Tier 4)', 'Warlock 4');" === False))
		echo "<span class = 'sans-warn'>Error updating Warlock OS</span><br />\n";
	else
		echo "'Warlock' OS updated<br />\n";

	if (ba_db_query ($link, "update `{$db_prefix}osps` set `ospName` = 'Paladin (Tier 1)', `ospShortName` = 'Paladin 1' where `ospName` = 'Paladin';insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Paladin (Tier 2)', 'Paladin 2');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Paladin (Tier 3)', 'Paladin 3');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Paladin (Tier 4)', 'Paladin 4');" === False))
		echo "<span class = 'sans-warn'>Error updating Paladin OS</span><br />\n";
	else
		echo "'Paladin' OS updated<br />\n";

	if (ba_db_query ($link, "update `{$db_prefix}osps` set `ospName` = 'Werecreature (Tier 1)', `ospShortName` = 'Were 1' where `ospName` = 'Werecreature';insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Werecreature (Tier 2)', 'Were 2');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Werecreature (Tier 3)', 'Were 3');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Werecreature (Tier 4)', 'Were 4');" === False))
		echo "<span class = 'sans-warn'>Error updating Were OS</span><br />\n";
	else
		echo "'Were' OS updated<br />\n";

	if (ba_db_query ($link, "update `{$db_prefix}osps` set `ospName` = 'Beguile (1)', `ospShortName` = 'Beguile 1' where `ospName` = 'Beguile <X>';insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Beguile (2)', 'Beguile 2');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Beguile (3)', 'Beguile 3');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Beguile (4)', 'Beguile 4');" === False))
		echo "<span class = 'sans-warn'>Error updating Beguile OS</span><br />\n";
	else
		echo "'Beguile' OS updated<br />\n";

	if (ba_db_query ($link, "insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Fearsome Aspect (1)', 'Fearsome Aspect 1');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Fearsome Aspect (2)', 'Fearsome Aspect 2');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Fearsome Aspect (3)', 'Fearsome Aspect 3');insert into `{$db_prefix}osps` (`ospName`, `ospShortName`) VALUES ('Fearsome Aspect (4)', 'Fearsome Aspect 4');" === False))
				echo "<span class = 'sans-warn'>Error updating Fearsome Aspect OS</span><br />\n";
	else
		echo "'Fearsome Aspect' OS updated<br />\n";

	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}config` add `cnUSE_QUEUE` smallint") === False)
		echo "<span class = 'sans-warn'>Error adding 'Use Queue' field to {$dbprefix}config</span><br />\n";
	else
		echo "'Use Queue' field added to: {$dbprefix}config<br />\n";

	echo "Setting default 'Use Queue' value<br />\n";
	if (ba_db_query ($link, "UPDATE `{$db_prefix}config` SET `cnUSE_QUEUE` = 0") === False)
		echo "<span class = 'sans-warn'>Error updating 'Use Queue' field</span><br />\n";
	else
		echo "'Use Queue' field defaults set<br />\n";

	echo "Adding Email preference fields<br />\n";
	if (ba_db_query ($link, "alter table `{$db_prefix}players` add column `plEmailICChange` tinyint(1) default '1'") === False)
		echo "<span class = 'sans-warn'>Error adding 'EmailICChange' field</span><br />\n";
	else
		echo "'EmailICChange' field created<br />\n";

	if (ba_db_query ($link, "alter table `{$db_prefix}players` add column `plEmailOOCChange` tinyint(1) default '1'") === False)
		echo "<span class = 'sans-warn'>Error adding 'EmailOOCChange' field</span><br />\n";
	else
		echo "'EmailOOCChange' field created<br />\n";

	if (ba_db_query ($link, "alter table `{$db_prefix}players` add column `plEmailPaymentReceived` tinyint(1) default '1'") === False)
		echo "<span class = 'sans-warn'>Error adding 'EmailPaymentReceived' field</span><br />\n";
	else
		echo "'EmailPaymentReceived' field created<br />\n";

	if (ba_db_query ($link, "alter table `{$db_prefix}players` add column `plEmailRemovedFromQueue` tinyint(1) default '1'") === False)
		echo "<span class = 'sans-warn'>Error adding 'EmailRemovedFromQueue' field</span><br />\n";
	else
		echo "'EmailRemovedFromQueue' field created<br />\n";

}

elseif ($_POST ['btnSubmit'] != '' && $_POST ['txtKey'] != CRYPT_KEY)
	echo "<span class = 'sans-warn'>Wrong value entered for CRYPT_KEY</span>";
?>
</p>

<p><a href = "./">Installation Tests &amp; Tools</a></p>

<?php
include ('../inc/inc_foot.php');
?>
